package com.dengyuanke.upms.logic.config.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;

import javax.servlet.http.HttpServletResponse;

/**
 * @author dengyuanke
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    /**
     * 自定义异常使用
     */
//    @Autowired
//    protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;

    @Autowired(required = true)
    private RemoteTokenServices remoteTokenServices;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().exceptionHandling()
                .authenticationEntryPoint(
                        (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                .and().authorizeRequests().antMatchers("/user/info/**","/log/**","/actuator/**").permitAll()
                .anyRequest().authenticated().and().httpBasic();
    }

//    @Override
//    @SneakyThrows
//    public void configure(ResourceServerSecurityConfigurer resources) {
//        resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint);
//    }

}
